from django.contrib.auth import get_user_model
from rest_framework import serializers

from .models import BlogCategory, Blog, BlogComment

User = get_user_model()


class BlogCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = BlogCategory
        fields = "__all__"


class BlogSerializer(serializers.ModelSerializer):
    author_name = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Blog
        fields = ["id",'title', 'content', 'category',"pub_time",'author_name']

    def get_author_name(self, obj):
        return obj.author.username


class BlogCommentSerializer(serializers.ModelSerializer):
    author_name = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = BlogComment
        fields = ['id', 'content', 'pub_time', 'author_name']

    def get_author_name(self, obj):
        return obj.author.username
